package com.hongning.railway.ui.main.mine.experience;

import android.app.Application;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableInt;
import android.databinding.ObservableList;
import android.support.annotation.NonNull;
import android.view.View;

import com.hongning.railway.R;
import com.hongning.railway.data.AppRepository;
import com.hongning.railway.entity.reqbean.ReqExperienceList;
import com.hongning.railway.entity.resbean.ResExperienceBean;
import com.hongning.railway.ui.main.fragments.college.exam.itemviewmodel.ExamItemViewModel;
import com.hongning.railway.ui.main.mine.experience.itemviewmodel.ExperienceItemViewModel;
import com.hongning.railway.util.Apputils;

import me.goldze.mvvmhabit.BR;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.http.ApiDisposableObserver;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class ExperienceFragmentViewModel extends BaseViewModel<AppRepository> {
    public ItemBinding<ExperienceItemViewModel> itemBinding  = ItemBinding.of(BR.viewModel, R.layout.item_experience);
    public ObservableList<ExperienceItemViewModel> items = new ObservableArrayList<>();
    public ObservableInt emptyvisiable = new ObservableInt(View.GONE);
    public BindingCommand createClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startContainerActivity(ExperiencePubOrEditFragment.class.getCanonicalName());
        }
    });
    public ExperienceFragmentViewModel(@NonNull Application application) {
        super(application);
    }

    public ExperienceFragmentViewModel(@NonNull Application application, AppRepository model) {
        super(application, model);
    }

    public void loadData(){
        items.clear();
        ReqExperienceList reqExperienceList = new ReqExperienceList();
        reqExperienceList.setUserId(Apputils.getStaffid());
        reqExperienceList.setPageNum(1);
        reqExperienceList.setPageSize(100);
        model.getExperienceList(reqExperienceList)
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .compose(RxUtils.schedulersTransformer())
                .subscribe(new ApiDisposableObserver<ResExperienceBean>() {
                    @Override
                    public void onResult(ResExperienceBean resExperienceBean) {
                        if (resExperienceBean.getSelectResult()==null||resExperienceBean.getSelectResult().isEmpty()){
                            emptyvisiable.set(View.VISIBLE);
                        }else {
                            emptyvisiable.set(View.GONE);
                            for (ResExperienceBean.SelectResultBean data:resExperienceBean.getSelectResult()) {
                                items.add(new ExperienceItemViewModel(ExperienceFragmentViewModel.this,data));
                            }
                        }
                    }
                });
    }
}
